var timer;
var register = {
	init: function(){
		this.checkFn();
		this.pageFun();
		this.sendMsgFn();
		this.refreshImgFn();
		
	},
	Interface:{
		handle:"http://ww.yc630.com/api/index.php/Buyer/Login/handleModifyPwd",  //重置密码
		sendMobileMsg:"http://ww.yc630.com/api/index.php/Buyer/Login/sendMobileMsg",   //电话
		sendmail:"http://ww.yc630.com/api/index.php/Buyer/Login/sendmail"        //邮箱
	},
	pageFun:function(){
		var locations=window.location.href.split("/").pop();
		if (locations=="forgetpwd.html") {
			register.Interface.handle="http://ww.yc630.com/api/index.php/Buyer/Login/handleModifyPwd"  //重置密码
		}else {
			register.Interface.handle="http://ww.yc630.com/api/index.php/Buyer/Login/handleRegister"  //注册			
		};
	},
	checkFn: function(){
		var	subBtn = $("#submit"),
			error_tips =$("#error_tips");
		subBtn.on("click",function(){
			var username = $("#username").val(),
				password = $("#password").val(),
				imageverify = $("#imageverify").val(),
				verify = $("#verify").val(),
				key=$("#hiddenVal").val();
			if( common.checkNull(username) || common.checkNull(password)){
				error_tips.show().text("手机号／邮箱号输入内容不能为空");
				return;
			};
			if(common.checkSpace(username) || common.checkSpace(password)){
				error_tips.show().text("输入内容不能包含空格");
				return;
			}
			if(common.checkUser(username)){
				error_tips.show().text("请输入正确的用户名");
				return ;
			}
			else if(common.checkPwd(password)){
				error_tips.show().text("请输入8到16位数字和字母组合格式的密码，区分大小写");
				return ;
			}else {
				error_tips.hide().text("");
				var regyx=/\@/g;
				var regtype ;
				if(regyx.test(username)){ //邮箱
					regtype=2
				}else{
					regtype=1
				}
				error_tips.hide().text("");
				common.ajaxFun(register.Interface.handle,"POST",{
					username: username,
					imageverify: imageverify,
					verify: verify,
					password: password,
					regtype: regtype,
					key:key
				},register.handleModifyPwdFun); //注册、重置密码
			};
		});	
	},
	handleModifyPwdFun:function(res){
		error_tips =$("#error_tips");
		if(res.code == 0){
			alert("注册／重置成功")
			var nowTime=new Date().getTime(),pwd="";
				pwd=md5($("#password").val());
				common.setCookie("userName",$("#username").val());
				common.setCookie("pwd",pwd);
				common.setCookie("check",1);

			window.location.href="./login.html"
  		}else{
  			error_tips.show().text(res.msg);
  		}
	},
	refreshImgFn: function(){
		var getImg= $("#getImg"),
			hiddenVal=$("#hiddenVal");
			//oldImg = getImg.attr("src");
			common.ajaxFun("http://ww.yc630.com/api/index.php/Buyer/Login/getImgVerify","POST",{},function(res){
				//console.log(res.data.image);
				getImg.attr("src", res.data.image);
				hiddenVal.val(res.data.key);
				
			}); 
		getImg.click(function(){
			common.ajaxFun("http://ww.yc630.com/api/index.php/Buyer/Login/getImgVerify","POST",{},function(res){
				getImg.attr("src", res.data.image);
				hiddenVal.val(res.data.key);
			}); 
			
		})
	},
	sendMsgFn: function(){
		var	error_tips =$("#error_tips");
		$("#getSmgCode").click(function(){
			var username = $("#username").val();
			if( common.checkNull(username)){
				error_tips.show().text("手机号／邮箱号输入内容不能为空");
				return;
			};
			if(common.checkUser(username)){
				error_tips.show().text("请输入正确的用户名");
				return ;
			}
			var regyx=/\@/g;
			var regtype ;
			if(regyx.test(username)){ //邮箱
				//console.log("邮箱")
				common.ajaxFun(register.Interface.sendmail,"POST",{
					email: username
				},register.sendMsg); //发送邮箱验证码
			}else{//手机
				//console.log("手机")
				common.ajaxFun(register.Interface.sendMobileMsg,"POST",{
					mobile: username
				},register.sendMsg); //发送手机验证码
			}
		})
	},
	sendMsg:function(res){
		error_tips =$("#error_tips");
		if(res.code == 0){
			error_tips.show().text( "验证码发送成功！");
			register.timerSetFn();
		}else{
			error_tips.show().text(res.msg);
		}
	},
	timerSetFn: function(){
	    var timerBtn = $("#getSmgCode");
	    if(!timerBtn.prop('disabled')){
	        timerFn(60, timerBtn);
	        timerBtn.attr('disabled', true);
	    }
	    function timerFn(sec,ele){
	        timer= setInterval(function(){
	            sec--;
	            ele.text('重新获取('+sec+')');
	            if(sec <=0){
	                register.clearFn();
	            }
	        }, 1000);
	    };
	},
	clearFn: function(){
	    clearInterval(timer);
	    $('#getSmgCode').text('获取验证码').removeAttr('disabled');
	    sec = 60;
	}
}
register.init();